package com.bbx.taxi.client.pay.weixin;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.official.passanger.Official_PayBuild;
import com.bbx.api.sdk.model.official.passanger.Return.Official_Pay;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Official_PayNet;
import com.bbx.api.sdk.net.passeger.conn.pay.WeiXinPayNet;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;
import com.bbx.taxi.official.wxapi.WXPayEntryActivity;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.taxi.client.Util.AppStarted;
import com.bbx.taxi.client.Util.VersionUtils;
import com.bbx.taxi.client.pay.alipay.PayAlipay.AlipayTask;
import com.tencent.mm.sdk.modelpay.PayReq;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import android.app.Activity;
import android.os.AsyncTask;

public class PayWeixin {
	Map<String, String> resultunifiedorder;
	PayReq req;
	IWXAPI msgApi;
	Activity context;
	public int price;
	public String order_id;
	public PayWeixin(Activity context,String order_id) {
		this.order_id=order_id;
		this.context = context;
	}

	public void pay() {
		MyApplication myApplication=MyApplication.getInstance();
		// 访问服务器获取支付订单
		Official_PayBuild mPayBuild = new Official_PayBuild(context);
		mPayBuild.passenger_id=myApplication.getUid();
		mPayBuild.order_id=order_id;
		switch(VersionUtils.getVersionType(myApplication)){
		case official:
			mPayBuild.gateway="wx_qz";
			break;
		case official_qz:
			mPayBuild.gateway="weixin";
			break;
		}
		WeixinPayTask mWeixinPayTask = new WeixinPayTask(context,mPayBuild);
		mWeixinPayTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 生成签名参数
	 */
	private void genPayReq(String data) {
		req = new PayReq();
		try {
			JSONObject json_data = new JSONObject(data);

			req.appId = json_data.getString("appid");
			req.partnerId = json_data.getString("partnerid");
			req.prepayId = json_data.getString("prepayid");
			req.packageValue = json_data.getString("package");
			req.nonceStr = json_data.getString("noncestr");
			req.timeStamp = json_data.getString("timestamp");

			List<NameValuePair> signParams = new LinkedList<NameValuePair>();
			signParams.add(new BasicNameValuePair("appid", req.appId));
			signParams.add(new BasicNameValuePair("noncestr", req.nonceStr));
			signParams.add(new BasicNameValuePair("package", req.packageValue));
			signParams.add(new BasicNameValuePair("partnerid", req.partnerId));
			signParams.add(new BasicNameValuePair("prepayid", req.prepayId));
			signParams.add(new BasicNameValuePair("timestamp", req.timeStamp));

			req.sign = json_data.getString("sign");
			sendPayReq(req.appId);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void sendPayReq(String appid) {

		msgApi = WXAPIFactory.createWXAPI(context, null);
		msgApi.registerApp(appid);
		msgApi.sendReq(req);
	}

	// 访问服务器获取支付订单
	// //////////////////////////////////////////////////
	public class WeixinPayTask extends BaseAsyncTask {
		Official_PayBuild mPayBuild;
		public WeixinPayTask(Activity context,Official_PayBuild mPayBuild) {
			super(context);
			this.mPayBuild=mPayBuild;
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			 String json = new JsonBuild().setModel(mPayBuild).getJson();
			return new Official_PayNet(context, json);
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				Official_Pay mPay = (Official_Pay) object;
				Official_Pay.Detail detail=mPay.getDetail();
				if (detail != null && detail.geteWay() != null
						&& detail.geteWay().equals("line")) {
					//支付完成
					if(WXPayEntryActivity.mOnWeiXinPayFinishListener!=null)WXPayEntryActivity.mOnWeiXinPayFinishListener.onWeiXinPayFinish(true);
				}else{
					if (detail!=null&& detail.getData() != null) {
						String data = detail.getData();
						genPayReq(data);
					}
				}
			}else{
				ToastUtil.showToast(context, R.string.no_network);
			}

		}

		@Override
		public void onFailed(int status, String msg) {
			// TODO Auto-generated method stub
			ToastUtil.showToast(context, R.string.no_network);
		}
	}

}
